#include "cvxDrawImage.h"

using cvx::Rgb8UImage;
using cvx::Bw8UImage;

void CvxDrawImage::drawLabel(const Mat &labelMap, int num, bool isColor)
{
	assert(num > 1 && num <= 255);
	assert(!labelMap.empty());
	assert(labelMap.channels() == 1);
	
	if(!isColor)
	{
		Mat showLabel = labelMap.clone();
		labelMap.convertTo(showLabel, CV_8UC1, 255.0/num);
		cv::imshow("label map", showLabel);
	}
	else
	{
		vector<Scalar> colors(num);
		for(int i = 0;i<num; i++)
		{
			colors[i][0] = rand()%256;
			colors[i][1] = rand()%256;
			colors[i][2] = rand()%256;
		}

		int h = labelMap.rows, w = labelMap.cols;
		Mat showLabel = cv::Mat::zeros(h, w, CV_8UC3);

		Rgb8UImage show_lab(&showLabel);
		for(int y  = 0; y<h; y++)
		{
			for(int x = 0; x<w; x++)
			{
				int lab_idx = 0;
				size_t eleSize = labelMap.elemSize();
				if(eleSize == 1)
				{
					lab_idx = labelMap.at<unsigned char>(y, x);
				}
				else if(eleSize == 4)
				{
					lab_idx = labelMap.at<int>(y, x);
				}
				show_lab[y][x].r = colors[lab_idx][0];
				show_lab[y][x].g = colors[lab_idx][1];
				show_lab[y][x].b = colors[lab_idx][2];
			}
		}
		cv::imshow("label map", showLabel);
	}
	
}